﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tradex.ComponentModel {
    
    /// <summary>
    /// This struct is a read only typed indexer and used to expose read only indexing behavior in
    /// properties. The acutal data has to be stored in an IList implementation.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable()]
    public struct ReadOnlyIndexer<T> {

        /// <summary>
        /// Public constructor, taking a List as source for the elements the ReadOnlyIndexer exposes.
        /// </summary>
        /// <param name="list"></param>
        public ReadOnlyIndexer(IList<T> list) {
            _List = list;
        }

        /// <summary>
        /// Gets an Item at the specific position from the Indexer.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T this[int index] {
            get { return _List[index]; }
        }

        /// <inheritdoc />
        public override bool Equals(object obj) {
            return _List.Equals(obj);
        }

        /// <inheritdoc />
        public override int GetHashCode() {
            return _List.GetHashCode();
        }

        IList<T> _List;

        /// <inheritdoc />
        public static bool operator == (ReadOnlyIndexer<T> left, ReadOnlyIndexer<T> right) {
            return left._List == right._List;
        }

        /// <inheritdoc />
        public static bool operator !=(ReadOnlyIndexer<T> left, ReadOnlyIndexer<T> right) {
            return left._List != right._List;
        }

    }

}
